/*
 * locTopics : id, title, updateTime, user_name, postCount
 */

var MapLocCtrl = dojo.declare(null, {
	//constructor/destructor
	constructor : mapLocCtrl_constructor,
	destroy : mapLocCtrl_destructor,
	
	//methods
	addTopic : mapLocCtrl_addTopic,
	init : mapLocCtrl_init
}); 

function mapLocCtrl_constructor(){
	var obj = this;
	
	obj._topicList = null;
	obj._curMapLoc = -1;
}

function mapLocCtrl_destructor(){
}

function mapLocCtrl_addTopic(){
	var obj = this;
	$("#add_topic #button_add_topic").unbind();
	function showMsg(item, msg, type){
		$.validationEngine.buildPrompt(item, msg, type);
		setTimeout("$.validationEngine.closePrompt('" + item + "');", 2000);
	}
	
	if ($("#topic_bound #input_title").val().length == 0){
		showMsg("#topic_bound #input_title", "标题不能为空。", "error");
		$("#add_topic #button_add_topic").click(function(){obj.addTopic();});
		return;
	}
	if ($("#topic_bound #input_content").val().length == 0){
		showMsg("#topic_bound #input_content", "话题内容不能为空。", "error");
		$("#add_topic #button_add_topic").click(function(){obj.addTopic();});
		return;
	}
	if ($("#topic_bound #input_content").val().length > 2048){
		showMsg("#topic_bound #input_content", "话题内容太长。", "error");
		$("#add_topic #button_add_topic").click(function(){obj.addTopic();});
		return;
	}
	showMsg("#topic_bound #button_add_topic", "正在发送，请稍候...", "load");
	
	var title = trimContent($("#topic_bound #input_title").val());
	var content = trimContent($("#topic_bound #input_content").val());
	$.post("/map/addSchoolMapLocTopic", {id : obj._curMapLoc, title : title, content : content}, function(data){
		if (data == "success"){
			$("#topic_bound #input_title").val("");
			$("#topic_bound #input_content").val("");
			obj._topicList.update();
			$("#add_topic #button_add_topic").click(function(){obj.addTopic();});
			showMsg("#topic_bound #button_add_topic", "话题发起成功！", "pass");
		}  else if (data == "too fast"){
			$("#add_topic #button_add_topic").click(function(){obj.addTopic();});
			showMsg("#topic_bound #button_add_topic", "对不起，您发起话题的速度太快了，请稍候再试。", "error");
		}  else {
			$("#add_topic #button_add_topic").click(function(){obj.addTopic();});
			showMsg("#topic_bound #button_add_topic", "话题发送失败。", "error");
		}
		$("#input_content").removeAttr("disabled");
	}, "json");
}

function mapLocCtrl_init(){
	var obj = this;
	
	obj._curMapLoc = $(".page_variable div[var_name='info']").attr("id");
	obj._topicList = new TabList();
	obj._topicList.init({baseItem : $("#topiclist"), listName : "地点话题", defaultTab : 0, 
		tabHidden : true,
		isTable : true,
		emptyHtml : ["<div style='padding-left:10px;'>暂无话题。</div>"],
		tabs : [["", "/map/getSchoolMapLocTopics", {"id" : obj._curMapLoc}, "/map/getSchoolMapLocTopicCount", {"id" : obj._curMapLoc}]],
		itemHtml : [$("#topiclist_example").html()],
		itemHtmlMapping : [[[0, "topic_id", "attr_id"], [1, "topic_title", "text"], [2, "topic_date", "text"], [3, "topic_user", "text"], [4, "topic_postcount", "text"]]],
		itemClicked : function(obj){
			var id = obj.find("#topic_id").attr("alt");
			window.location = "/map/maploc/topic?id=" + id;
		}
	});
	
	$("#add_topic #button_add_topic").unbind();
	$("#add_topic #button_add_topic").click(function(){
		obj.addTopic();
	});
}

if (dojo.getObject("mapLocCtrl") == null) var mapLocCtrl = new MapLocCtrl;
$(document).ready(function(){
	mapLocCtrl.init();
});
